{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9b870557-a585-41f1-bba7-144c5dacc388",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-07T07:21:20.384780Z",
     "iopub.status.busy": "2022-05-07T07:21:20.384468Z",
     "iopub.status.idle": "2022-05-07T07:21:21.205973Z",
     "shell.execute_reply": "2022-05-07T07:21:21.205338Z",
     "shell.execute_reply.started": "2022-05-07T07:21:20.384710Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99abd16d",
   "metadata": {},
   "source": [
    "## 小结\n",
    "1. 层级索引\n",
    "   * 索引嵌套的结构\n",
    "2. 选取\n",
    "   1. 使用外层索引选取数据\n",
    "   2. 使用内层索引选取数据\n",
    "3. 交换\n",
    "   1. 使用swaplevel交换内外层索引\n",
    "   2. 先交换内外层索引，再对所有排序\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5dd8582-ba40-4b51-8321-41fde104539d",
   "metadata": {},
   "source": [
    "## 一、层级索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fb568a04-a527-4dc9-a6d1-0b279ff84b76",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-07T07:22:54.932612Z",
     "iopub.status.busy": "2022-05-07T07:22:54.932249Z",
     "iopub.status.idle": "2022-05-07T07:22:54.939557Z",
     "shell.execute_reply": "2022-05-07T07:22:54.938362Z",
     "shell.execute_reply.started": "2022-05-07T07:22:54.932587Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0.203040\n",
      "a    0.970109\n",
      "a    1.601368\n",
      "b    0.293078\n",
      "b   -1.177318\n",
      "b   -2.028686\n",
      "c   -1.020078\n",
      "c   -0.608736\n",
      "c   -0.729128\n",
      "d   -1.413197\n",
      "d    1.065575\n",
      "d   -0.758010\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 创建Series对象\n",
    "s1 = pd.Series(\n",
    "    np.random.randn(12),\n",
    "    index=[\"a\", \"a\", \"a\", \"b\", \"b\", \"b\", \"c\", \"c\", \"c\", \"d\", \"d\", \"d\"],\n",
    ")\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a6a0a983-6518-43da-b4fd-b7fd22270203",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-07T07:37:09.262646Z",
     "iopub.status.busy": "2022-05-07T07:37:09.262394Z",
     "iopub.status.idle": "2022-05-07T07:37:09.270941Z",
     "shell.execute_reply": "2022-05-07T07:37:09.269811Z",
     "shell.execute_reply.started": "2022-05-07T07:37:09.262618Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a  0    0.206270\n",
      "   1    0.501584\n",
      "   2   -0.641401\n",
      "b  0   -2.368942\n",
      "   1   -0.126621\n",
      "   2    0.457002\n",
      "c  0    0.431677\n",
      "   1    0.508452\n",
      "   2   -0.157476\n",
      "d  0   -1.937860\n",
      "   1   -0.819255\n",
      "   2    0.117493\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 创建Series对象，设置层级索引\n",
    "s1 = pd.Series(\n",
    "    np.random.randn(12),\n",
    "    index=[\n",
    "        [\"a\", \"a\", \"a\", \"b\", \"b\", \"b\", \"c\", \"c\", \"c\", \"d\", \"d\", \"d\"],\n",
    "        [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],\n",
    "    ],\n",
    ")\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "89364dd3-dd4c-4ed7-a0d9-443bf1f76c85",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-07T10:44:27.543537Z",
     "iopub.status.busy": "2022-05-07T10:44:27.543283Z",
     "iopub.status.idle": "2022-05-07T10:44:27.547992Z",
     "shell.execute_reply": "2022-05-07T10:44:27.547362Z",
     "shell.execute_reply.started": "2022-05-07T10:44:27.543510Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.indexes.multi.MultiIndex'>\n"
     ]
    }
   ],
   "source": [
    "# 查看s1的索引类型\n",
    "print(type(s1.index))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "40b07b68-1eda-4316-baba-dd28baf67390",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-07T10:51:43.364479Z",
     "iopub.status.busy": "2022-05-07T10:51:43.364232Z",
     "iopub.status.idle": "2022-05-07T10:51:43.369090Z",
     "shell.execute_reply": "2022-05-07T10:51:43.368020Z",
     "shell.execute_reply.started": "2022-05-07T10:51:43.364452Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MultiIndex([('a', 0),\n",
      "            ('a', 1),\n",
      "            ('a', 2),\n",
      "            ('b', 0),\n",
      "            ('b', 1),\n",
      "            ('b', 2),\n",
      "            ('c', 0),\n",
      "            ('c', 1),\n",
      "            ('c', 2),\n",
      "            ('d', 0),\n",
      "            ('d', 1),\n",
      "            ('d', 2)],\n",
      "           )\n"
     ]
    }
   ],
   "source": [
    "# 查看s1的索引\n",
    "print(s1.index)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e80f36d-2087-4898-81dc-ab9d83edc463",
   "metadata": {},
   "source": [
    "## 二、选取"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c67fdbb8-7b53-49fc-8c89-5df4d2d79a7c",
   "metadata": {},
   "source": [
    "### 2.1 使用外层索引选取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "01f75b72-8c15-4889-8b80-f980736f639b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-07T11:02:20.866658Z",
     "iopub.status.busy": "2022-05-07T11:02:20.866316Z",
     "iopub.status.idle": "2022-05-07T11:02:20.871778Z",
     "shell.execute_reply": "2022-05-07T11:02:20.870518Z",
     "shell.execute_reply.started": "2022-05-07T11:02:20.866631Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.206270\n",
      "1    0.501584\n",
      "2   -0.641401\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 使用外层索引选取数据\n",
    "print(s1[\"a\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b22ab91d-4415-43f3-9704-394485788ea5",
   "metadata": {},
   "source": [
    "### 2.2 使用内层索引选取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "da9839b1-af24-4c12-8ed6-8dbf1443c152",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-07T11:08:12.825389Z",
     "iopub.status.busy": "2022-05-07T11:08:12.825141Z",
     "iopub.status.idle": "2022-05-07T11:08:12.830865Z",
     "shell.execute_reply": "2022-05-07T11:08:12.830122Z",
     "shell.execute_reply.started": "2022-05-07T11:08:12.825363Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5015836914988695"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用内层索引选取数据，考虑外层索引\n",
    "s1[\"a\",1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9cc4c610-6fad-4ba1-a7d7-474decaaf144",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-07T11:12:30.895283Z",
     "iopub.status.busy": "2022-05-07T11:12:30.894987Z",
     "iopub.status.idle": "2022-05-07T11:12:30.901846Z",
     "shell.execute_reply": "2022-05-07T11:12:30.900773Z",
     "shell.execute_reply.started": "2022-05-07T11:12:30.895256Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    0.501584\n",
       "b   -0.126621\n",
       "c    0.508452\n",
       "d   -0.819255\n",
       "dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用内层索引选取数据，不考虑外层索引\n",
    "s1[:,1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0204fbc7-adf8-450d-82fa-7640ca142232",
   "metadata": {},
   "source": [
    "## 三、交换"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fe096b6-ef8f-464b-9201-3fb4e444a376",
   "metadata": {},
   "source": [
    "### 3.1 使用swaplevel交换内外层索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "95ad2079-c995-45ab-aab1-2b899da0357b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-08T01:38:18.878682Z",
     "iopub.status.busy": "2022-05-08T01:38:18.878427Z",
     "iopub.status.idle": "2022-05-08T01:38:18.900539Z",
     "shell.execute_reply": "2022-05-08T01:38:18.899628Z",
     "shell.execute_reply.started": "2022-05-08T01:38:18.878654Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0  a    0.206270\n",
       "1  a    0.501584\n",
       "2  a   -0.641401\n",
       "0  b   -2.368942\n",
       "1  b   -0.126621\n",
       "2  b    0.457002\n",
       "0  c    0.431677\n",
       "1  c    0.508452\n",
       "2  c   -0.157476\n",
       "0  d   -1.937860\n",
       "1  d   -0.819255\n",
       "2  d    0.117493\n",
       "dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# swaplevel交换内外层索引\n",
    "s1.swaplevel()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36d1a988-417c-46ad-8649-6ddd0f31c6be",
   "metadata": {},
   "source": [
    "### 3.2 先交换，后对索引排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f25d05c8-22d3-4c9e-adde-1cbf07f5953a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-08T01:59:01.298140Z",
     "iopub.status.busy": "2022-05-08T01:59:01.297882Z",
     "iopub.status.idle": "2022-05-08T01:59:01.306296Z",
     "shell.execute_reply": "2022-05-08T01:59:01.305678Z",
     "shell.execute_reply.started": "2022-05-08T01:59:01.298112Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0  a    0.206270\n",
       "   b   -2.368942\n",
       "   c    0.431677\n",
       "   d   -1.937860\n",
       "1  a    0.501584\n",
       "   b   -0.126621\n",
       "   c    0.508452\n",
       "   d   -0.819255\n",
       "2  a   -0.641401\n",
       "   b    0.457002\n",
       "   c   -0.157476\n",
       "   d    0.117493\n",
       "dtype: float64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# swaplevel交换内外层索引\n",
    "# sort_index对所有排序，默认会从外层向内层依次排序\n",
    "s1.swaplevel().sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5133f67-5f09-4131-8fd1-b0b83c4fa5b2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
